package com.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.constants.ErrorCodeEnum;
import com.example.constants.SuccessCodeEnum;
import com.example.domain.Dorm;
import com.example.domain.User;
import com.example.handler.BusinessErrorException;
import com.example.service.UserService;
import com.example.untils.annotation.AopLog;
import com.example.vo.UserVo;
import com.example.vo.res.JsonResult;
import com.example.vo.res.UserJsonResult;

@Api(description = "用户接口")
@RestController
@RequestMapping(value = "/users")
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation(value = "获取用户列表", notes = "", response = UserJsonResult.class)
    @RequestMapping(value = { "" }, method = RequestMethod.GET)
    public JsonResult getUserList() {
        return new JsonResult(SuccessCodeEnum.SUCCESS, userService.findAll());
    }

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户",
        response = UserJsonResult.class)
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true,
        dataType = "UserVo")
    @RequestMapping(value = "", method = RequestMethod.POST)
    @AopLog(operation = "创建用户")
    public JsonResult postUser(@RequestBody UserVo user) {
        User userdo = new User();
        BeanUtils.copyProperties(user, userdo);
        userdo.setDorm(new Dorm(user.getDormId()));
        userService.save(userdo);
        return new JsonResult();
    }

    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息",
        response = UserJsonResult.class)
    @ApiImplicitParam(name = "id", value = "用户ID", required = true,
        dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult getUser(@PathVariable Long id) {
        return new JsonResult(SuccessCodeEnum.SUCCESS, userService.findOne(id));
    }

    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息",
        response = UserJsonResult.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "age", value = "年龄", required = true,
            dataType = "Long", paramType = "path"),
        @ApiImplicitParam(name = "name", value = "姓名", required = true,
            dataType = "String", paramType = "path") })
    @RequestMapping(value = "/{age}/{name}", method = RequestMethod.GET)
    public JsonResult getUser(@PathVariable String name,
            @PathVariable Integer age) {
        return new JsonResult(SuccessCodeEnum.SUCCESS,
                userService.findByNameAndAge(name, age));
    }

    @ApiOperation(value = "更新用户详细信息",
        notes = "根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息",
        response = UserJsonResult.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "用户ID", required = true,
            dataType = "Long", paramType = "path"),
        @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true,
            dataType = "UserVo") })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public JsonResult putUser(@PathVariable Long id, @RequestBody UserVo user) {
        User userdo = userService.findOne(id);
        BeanUtils.copyProperties(user, userdo, "id");
        userdo.setDorm(new Dorm(user.getDormId()));
        userService.save(userdo);
        return new JsonResult();
    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象",
        response = UserJsonResult.class)
    @ApiImplicitParam(name = "id", value = "用户ID", required = true,
        dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id) {
        userService.remove(id);
        return "success";
    }

    @ApiOperation(value = "删除用户(测试系统异常)", notes = "内部异常",
        response = UserJsonResult.class)
    @ApiImplicitParam(name = "id", value = "用户ID", required = true,
        dataType = "Long", paramType = "path")
    @RequestMapping(value = "/err/{id}", method = RequestMethod.DELETE)
    public JsonResult testException(@PathVariable Long id) {
        throw new RuntimeException("测试服务器发生异常");

    }

    @ApiOperation(value = "删除用户(测试业务异常)", notes = "内部异常",
        response = UserJsonResult.class)
    @ApiImplicitParam(name = "id", value = "用户ID", required = true,
        dataType = "Long", paramType = "path")
    @RequestMapping(value = "/berr/{id}", method = RequestMethod.DELETE)
    public JsonResult testBException(@PathVariable Long id)
        throws BusinessErrorException {
        throw new BusinessErrorException(ErrorCodeEnum.NOT_SUPPORTED);

    }
}